package com.dhcc.bpm.modules.bbs.controller;

import com.dhcc.bpm.modules.bbs.vo.BbsHomePageVO2;
import com.dhcc.bpm.modules.common.entity.Plupload;
import com.dhcc.bpm.modules.common.service.IPluploadService;
import com.dhcc.bpm.modules.bbs.entity.BbsHomePage;
import io.swagger.annotations.Api;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.bbs.vo.BbsHomePageVO;
import com.dhcc.bpm.modules.bbs.service.IBbsHomePageService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 * 门户首页 控制器
 *
 * @author donghang
 * @since 2020-03-31
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-bbs/bbshomepage")
@Api(value = "门户首页", tags = "门户首页接口")
public class BbsHomePageController extends BladeController {

	private IBbsHomePageService bbsHomePageService;

	private IPluploadService pluploadService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入bbsHomePage")
	public R<BbsHomePage> detail(BbsHomePage bbsHomePage) {
		BbsHomePage detail = bbsHomePageService.getOne(Condition.getQueryWrapper(bbsHomePage));
		Plupload plupload = pluploadService.getById(detail.getBusId());
		BbsHomePageVO homePageVO = new BbsHomePageVO();
		BeanUtil.copy(detail, homePageVO);
		if(plupload != null){
			homePageVO.setFileFullPath(plupload.getFileFullpath());
			homePageVO.setStorageType(plupload.getStorageType());
			homePageVO.setFileRealname(plupload.getFileRealname());
		}
		return R.data(homePageVO);
	}

	/**
	 * 分页 门户首页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入bbsHomePage")
	public R<IPage<BbsHomePageVO2>> list(BbsHomePageVO2 bbsHomePage, Query query) {
		IPage<BbsHomePageVO2> pages = bbsHomePageService.selectBbsHomePage2(Condition.getPage(query), bbsHomePage);
		return R.data(pages);
	}

	/**
	 * 自定义分页 门户首页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入bbsHomePage")
	public R<IPage<BbsHomePageVO>> page(BbsHomePageVO bbsHomePage, Query query) {
		IPage<BbsHomePageVO> pages = bbsHomePageService.selectBbsHomePagePage(Condition.getPage(query), bbsHomePage);
		return R.data(pages);
	}

	/**
	 * 新增 门户首页
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入bbsHomePage")
	public R save(@Valid @RequestBody BbsHomePage bbsHomePage) {
		return R.status(bbsHomePageService.save(bbsHomePage));
	}

	/**
	 * 修改 门户首页
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入bbsHomePage")
	public R update(@Valid @RequestBody BbsHomePage bbsHomePage) {
		return R.status(bbsHomePageService.updateById(bbsHomePage));
	}

	/**
	 * 新增或修改 门户首页
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入bbsHomePage")
	public R submit(@Valid @RequestBody BbsHomePage bbsHomePage) {
		return R.status(bbsHomePageService.saveOrUpdate(bbsHomePage));
	}

	
	/**
	 * 删除 门户首页
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(bbsHomePageService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * @author: 陈泽
	 * @date: 2019/12/11 9:43
	 * @description: 修改时删除图片
	 * @param: [id, busId]
	 * @return: org.springblade.core.tool.api.R
	 */
	@PostMapping("/deleteImage")
	public R deleteImage(Long id, Long busId){
		pluploadService.fileDelete(busId);
		BbsHomePage homePage = new BbsHomePage();
		homePage.setId(id);
		homePage.setBusId(-1L);
		return R.status(bbsHomePageService.updateById(homePage));
	}
	
}
